package com.tdkj.task;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tdkj.model.shop.BeanGoodAuction;
import com.tdkj.service.shop.IBeanGoodAuctionService;

public class GoodAuctionResultDetermineTask {
	 /** Logger */
    private static final Logger logger = LoggerFactory.getLogger(GoodAuctionResultDetermineTask.class);
    
    private IBeanGoodAuctionService goodAuctionService;
 
    /**
	 * @param goodAuctionService the goodAuctionService to set
	 */
    @Resource
	public void setGoodAuctionService(IBeanGoodAuctionService goodAuctionService) {
		this.goodAuctionService = goodAuctionService;
	}



	/**
     * 业务逻辑处理
     */
    @SuppressWarnings("unchecked")
	public void execute() {
    	while(true){
    		Long sleepMillis = 2L*60*1000;
    			
			List<BeanGoodAuction> auctions = goodAuctionService
			.findByProperty("state", "0", BeanGoodAuction.class, "endTime", "asc");
    		
			for(BeanGoodAuction auction : auctions){
				Long currentTime = System.currentTimeMillis();
				if(auction.getEndTime() != null){
					if(currentTime > auction.getEndTime().getTime()){
						goodAuctionService.finish(auction.getId());
					}else{
						sleepMillis = auction.getEndTime().getTime() - currentTime;
						break;
					}
				}
				
			}
			
    		try {
    			//睡眠时间
				Thread.sleep(sleepMillis);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
    	}
    }
}
